﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace SilverlightDemo
{
    public enum ScalingModalExpandCollapseAnimation
    {
        None = 0,
        ScaleI,
        ScaleO,
        ScaleV,
        ScaleH,
        RotateCW,
        RotateCCW,
        SlideR,
        SlideL,
        SlideT,
        SlideB,
        Fade,
        Twist,
        Skew1,
        Skew2,
        Skew3,
        Skew4,
        Flip,
        Random
    }


    public partial class ScalingModal : UserControl
    {
        private bool _isExpanded;
        private ContentControl _disableControl = null;
        private FrameworkElement _innerControl;
        private static ScalingModal _instance;

        protected ScalingModal()
        {
            InitializeComponent();
            _isExpanded = false;
            IsHitTestVisible = false;
        }

        static ScalingModal()
        {
            _instance = new ScalingModal();
        }

        public bool IsExpanded { get { return _isExpanded; } }

        public ScalingModalExpandCollapseAnimation Animation { get; set; }

        public double BackgroundOpacity
        {
            get { return DisableRectangle.Opacity; }
            set { DisableRectangle.Opacity = value; }
        }

        public static ScalingModal Instance
        {
            get { return _instance; }
        }

        public void Initialize(Panel parentPanel)
        {
            Initialize(parentPanel, null);
        }

        public void Initialize(Panel parentPanel, ContentControl disableControl)
        {
            _disableControl = disableControl;
            Panel parent = parentPanel;
            parent.Children.Add(this);
        }


        public void Expand(FrameworkElement innerControl, ScalingModalExpandCollapseAnimation animationToUse)
        {
            if (_disableControl != null)
                _disableControl.IsEnabled = false;
            _innerControl = innerControl;

            ModalCanvas.Width = innerControl.Width;
            ModalCanvas.Height = innerControl.Height;
            ModalCanvas.Children.Clear();
            ModalCanvas.Children.Add(innerControl);

            this.IsHitTestVisible = true;

            RunExpandAnimation(animationToUse);

            _isExpanded = true;
        }

        public void Expand(FrameworkElement innerControl)
        {
            Expand(innerControl, Animation);
        }

        public void Collapse(ScalingModalExpandCollapseAnimation animationToUse)
        {
            if (_disableControl != null)
                _disableControl.IsEnabled = true;
            this.IsHitTestVisible = false;
            _isExpanded = false;
            RunCollapseAnimation(Animation);
        }

        public void Collapse()
        {
            Collapse(Animation);
        }

        private void RunExpandAnimation(ScalingModalExpandCollapseAnimation animation)
        {
            ResetTransformation();
            if (animation == ScalingModalExpandCollapseAnimation.Random)
                RunExpandAnimation(GetRandomAnimationEnumeration());
            else
                RunAnimation(this, animation.ToString() + "In");
        }

        private void RunAnimation(ScalingModal scalingModal, string key)
        {
            Storyboard s = Resources[key] as Storyboard;
            s.Begin();
        }

        private void ResetTransformation()
        {
            RunAnimation(this, "Reset");
        }

        private static ScalingModalExpandCollapseAnimation GetRandomAnimationEnumeration()
        {
            return (ScalingModalExpandCollapseAnimation)(new Random().Next(1,
                ((int)(ScalingModalExpandCollapseAnimation.Random)) - 1));
        }
        
        private void RunCollapseAnimation(ScalingModalExpandCollapseAnimation animation)
        {
            ResetTransformation();
            if (animation == ScalingModalExpandCollapseAnimation.Random)
                RunCollapseAnimation(GetRandomAnimationEnumeration());
            else
                RunAnimation(this, animation.ToString() + "Out");
        }

        private void expandingOpacityAnimation_Completed(object sender, EventArgs e)
        {
        }

        private void collapsingOpacityAnimation_Completed(object sender, EventArgs e)
        {
            _innerControl = null;
            ModalCanvas.Children.Clear();
        }

    }
}
